﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using HF.Framework.DataClientDBAgent;
using HF.WorkFlow.DataContract;
using HF.Framework.SqlDataContract;
namespace HF.Forms.Data
{
    /// <summary>
    /// 数据访问类MainForm。
    /// </summary>
    public class MainForm
    {
        #region  私有字段
        private SqlDataItem sqlDataItem = new SqlDataItem();
        private string[] fieldList = { "MainFormId", "Caption", "Description", "FormType" };
        private string tableName = "HF_MainForms";
        private string keyField = "MainFormId";
        private string sqlString = "";

        #endregion

        #region  私有方法
        private void setParameter(MainFormEntity entity)
        {
            sqlDataItem.ParameterList.Clear();
            sqlDataItem.AppendParameter("@MainFormId", entity.MainFormId, typeof(string));
            sqlDataItem.AppendParameter("@Caption", entity.Caption, typeof(string));
            sqlDataItem.AppendParameter("@Description", entity.Description, typeof(string));
            sqlDataItem.AppendParameter("@FormType", entity.FormType, typeof(string));
        }

        private void setInsertSql()
        {
            string tmpValueList = "";
            string tmpFieldName = "";
            sqlString = "insert into " + tableName + "(";
            int tmpInt = this.fieldList.Length;
            for (int i = 0; i < tmpInt - 1; i++)
            {
                tmpFieldName = fieldList[i].ToString();
                sqlString = sqlString + tmpFieldName + ", ";
                tmpValueList = tmpValueList + "@" + tmpFieldName + ",";
            }
            tmpFieldName = this.fieldList[tmpInt - 1].ToString();
            sqlString = sqlString + tmpFieldName;
            tmpValueList = tmpValueList + "@" + tmpFieldName;
            this.sqlString = sqlString + ")values(" + tmpValueList + ")";
            sqlDataItem.CommandText = sqlString;
        }

        private void setUpdateSql()
        {
            string tmpFieldName = "";
            int tmpInt = this.fieldList.Length;
            sqlString = "update " + tableName + " set ";
            for (int i = 0; i < tmpInt - 1; i++)
            {
                tmpFieldName = fieldList[i].ToString();
                sqlString = sqlString + tmpFieldName + " = @" + tmpFieldName + ", ";
            }
            tmpFieldName = this.fieldList[tmpInt - 1].ToString();
            sqlString = sqlString + tmpFieldName + " = @" + tmpFieldName;
            sqlString = sqlString + " where " + keyField + "=@" + this.keyField;
            sqlDataItem.CommandText = sqlString;
        }

        public MainFormEntity GetMainFormEntity(DataRow dr)
        {
            MainFormEntity entity = new MainFormEntity();
            entity.MainFormId = dr["MainFormId"].ToString();
            entity.Caption = dr["Caption"].ToString();
            entity.Description = dr["Description"].ToString();
            entity.FormType = dr["FormType"].ToString();
            return entity;
        }

        #endregion
        #region  构造函数
        public MainForm()
        { }
        public static MainForm GetInstance()
        {
            return new MainForm();
        }
        #endregion
        #region  公有方法
        #region  基本方法
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="entity">数据实体</param>
        /// <returns>影响的记录条数</returns>
        public int Insert(MainFormEntity entity)
        {
            try
            {
                setInsertSql();//设定insert语句
                setParameter(entity);//设定参数
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteNonQuery(sqlDataItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity">数据实体</param>
        /// <returns>影响的记录条数</returns>
        public int Update(MainFormEntity entity)
        {
            try
            {
                setUpdateSql();//设定insert语句
                setParameter(entity);//设定参数
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteNonQuery(sqlDataItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string mainFormId)
        {
            string tmpSql = "select * from " + tableName + " where " + keyField + "=@" + keyField;
            SqlDataItem sqlItem = new SqlDataItem();
            sqlItem.CommandText = tmpSql;
            sqlItem.AppendParameter("@" + keyField, mainFormId);
            ClientDBAgent agent = new ClientDBAgent();
            return agent.RecordExists(sqlItem);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="mainFormId">主键Id</param>
        /// <returns>影响的记录条数</returns>
        public void Delete(string mainFormId)
        {
            try
            {
                string tmpSql = "delete from " + tableName + " where  " + keyField + "=@" + keyField;
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpSql;
                sqlItem.AppendParameter("@" + keyField, mainFormId);
                ClientDBAgent agent = new ClientDBAgent();
                agent.ExecuteNonQuery(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取一条数据记录
        /// </summary>
        /// <param name="MainFormId">主键Id</param>
        /// <returns>DataTable</returns>
        public DataTable GetMainFormTable(string mainFormId)
        {
            string tmpSql = "select * from " + tableName + " where " + keyField + "=@" + keyField;
            try
            {
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpSql;
                sqlItem.AppendParameter("@" + keyField, mainFormId);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取一个数据实体
        /// </summary>
        /// <param name="mainFormId">主表单Id</param>
        /// <returns>DataTable</returns>
        public MainFormEntity GetMainFormEntity(string mainFormId)
        {
            DataTable dt = GetMainFormTable(mainFormId);
            if (dt != null && dt.Rows.Count > 0)
            {
                return GetMainFormEntity(dt.Rows[0]);
            }
            return null;
        }

        #endregion
        #region  扩展方法
        //此处添加扩展方法

        /// <summary>
        /// 获得子表单隶属的主表单
        /// </summary>
        ///  <param name="childFormId">子表单Id</param>
        /// <returns>DataTable</returns>
        public  DataTable GetMainFormsOfChild(string childFormId)
        {
            try
            {
                string tmpStr = "select mfm.* from HF_MainChildForm mcf left join HF_MainForms mfm on mfm.MainformId=mcf.MainFormId "+
                               " where mcf.ChildFormId=@ChildFormId order by mfm.caption";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpStr;
                sqlItem.AppendParameter("@childFormId", childFormId);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获得子表单隶属的主表单
        /// </summary>
        ///  <param name="childFormId">子表单Id</param>
        /// <returns>DataTable</returns>
        public List<MainFormEntity> GetMainFormEntitysOfChild(string childFormId)
        {
            List<MainFormEntity> result = new List<MainFormEntity>();
            DataTable dt = GetMainFormsOfChild(childFormId);
            try
            {
                foreach (DataRow dr in dt.Rows)
                {
                    MainFormEntity entity = GetMainFormEntity(dr);
                    result.Add(entity);
                }
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获得所有主表单
        /// </summary>
        /// <returns>DataTable</returns>
        public  DataTable GetAllMainForms()
        {
            try
            {
                string tmpStr = "select * from HF_MainForms order by Caption";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpStr;
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获得所有主表单
        /// </summary>
        /// <returns>Entitys</returns>
        public List<MainFormEntity> GetAllMainFormEntitys()
        {
            List<MainFormEntity> result = new List<MainFormEntity>();
            DataTable dt = GetAllMainForms();
            try
            {
                foreach (DataRow dr in dt.Rows)
                {
                    MainFormEntity entity = GetMainFormEntity(dr);
                    result.Add(entity);
                }
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获得主表单
        /// </summary>
        /// <param name="caption">主表单名称</param>
        /// <returns>DataTable</returns>
        public  DataTable GetMainFormsLikeCaption(string caption)
        {
            try
            {
                string tmpStr = "select * from HF_MainForms where Caption like @Caption";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpStr;
                sqlItem.AppendParameter("@Caption", "%" + caption + "%");
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获得子表单隶属的主表单
        /// </summary>
        ///  <param name="caption">主表单名称</param>
        /// <returns>Enitys</returns>
        public List<MainFormEntity> GetMainFormEntitysLikeCaption(string caption)
        {
            List<MainFormEntity> result = new List<MainFormEntity>();
            DataTable dt = GetMainFormsLikeCaption(caption);
            try
            {
                foreach (DataRow dr in dt.Rows)
                {
                    MainFormEntity entity = GetMainFormEntity(dr);
                    result.Add(entity);
                }
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获得流程所有主表单信息
        /// </summary>
        /// <param name="workFlowId">流程Id</param>
        /// <returns>DataTable</returns>
        public  DataTable GetMainFormsOfWorkFlow(string workFlowId)
        {
            try
            {
                string tmpStr = "select distinct mf.* from HF_WorkTaskForm wtf left join HF_MainChildForm wcf on wtf.MainFormId=wcf.MainFormId " +
                                " left join HF_MainForms mf on mf.MainFormId=wcf.MainFormId " +
                                "         where wtf.workflowid=@workFlowId ";

                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpStr;
                sqlItem.AppendParameter("@workFlowId", workFlowId);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获得任务节点配置的主表单信息
        /// </summary>
        /// <param name="workFlowId">流程Id</param>
        /// <returns>DataTable</returns>
        public DataTable GetMainFormOfWorkTask(string workTaskId)
        {
            try
            {
                string tmpStr = "select distinct mf.* from HF_WorkTaskForm wtf left join HF_MainChildForm wcf on wtf.MainFormId=wcf.MainFormId " +
                                " left join HF_MainForms mf on mf.MainFormId=wcf.MainFormId " +
                                "         where wtf.workTaskId=@workTaskId ";

                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpStr;
                sqlItem.AppendParameter("@workTaskId", workTaskId);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获得任务节点配置的主表单信息,一个任务节点只允许挂接一个主表单
        /// </summary>
        /// <param name="workFlowId">流程Id</param>
        /// <returns>DataTable</returns>
        public MainFormEntity GetMainFormEntityOfWorkTask(string workTaskId)
        {
            MainFormEntity result = null;
            try
            {
                DataTable dt = GetMainFormOfWorkTask(workTaskId);
                if (dt != null && dt.Rows.Count > 0)
                {
                    result = GetMainFormEntity(dt.Rows[0]);
                }
                return result;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
        #endregion
    }
}

